package fr.imag.ufrima.ctte.gui.views;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import fr.imag.ufrima.ctte.ImagCTTE;
import fr.imag.ufrima.ctte.Task;
import fr.imag.ufrima.ctte.gui.dialogs.ChangeTextDialog;

public class TaskPropertiesSubView extends SubView {
	private Task task;
	/**
	 * Constructor
	 */
	public TaskPropertiesSubView(ImagCTTE imagCTTE){		
		super(imagCTTE, "Task Properties");
		InitComponents();
		setTask(imagCTTE.getCurrentTaskDiagram().getSelectedTask());
		
		// listeners
		txtIdentifier.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent arg0) {
        		String txt = txtIdentifier.getText();
        		txt.trim();
        		if (txt.equals("") != true){
        			getImagCTTE().getCurrentTaskDiagram().getSelectedTask().setIdentifier(txt);
        			txtIdentifier.setText(txt);
        		}
        	}
        });
		txtName.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent arg0) {
        		String txt = txtName.getText();
        		txt.trim();
        		if (txt.equals("") != true){
        			getImagCTTE().getCurrentTaskDiagram().getSelectedTask().setName(txt);
        			txtName.setText(txt);
        		}
        	}
        });
		txtDescription.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent arg0) {
        		String txt = txtDescription.getText();
        		txt.trim();
        		if (txt.equals("") != true){
        			getImagCTTE().getCurrentTaskDiagram().getSelectedTask().setDescription(txt);
        			txtDescription.setText(txt);
        		}
        	}
        });
		/*
		txtPrecondition.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent arg0) {
        		String txt = txtPrecondition.getText();
        		txt.trim();
        		if (txt.equals("") != true){
        			getImagCTTE().getCurrentTaskDiagram().getSelectedTask().setPreCondition(txt);
        			txtPrecondition.setText(txt);
        		}
        	}
        });*/
		
		txtPrecondition.addPropertyChangeListener(new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent arg0) {							
				//JOptionPane.showMessageDialog(null, arg0.getPropertyName());
			}
		});
		
		ckbIteratif.addChangeListener(new ChangeListener(){			
			public void stateChanged(ChangeEvent arg0) {
				getImagCTTE().getCurrentTaskDiagram().getSelectedTask().setIteratif(ckbIteratif.isSelected());
			}
		});
		ckbOptional.addChangeListener(new ChangeListener(){			
			public void stateChanged(ChangeEvent arg0) {
				getImagCTTE().getCurrentTaskDiagram().getSelectedTask().setOptional(ckbOptional.isSelected());
			}
		});			
		
	}
	public void setTask(Task task){
		this.task = task;
		if (task != null) {
			txtIdentifier.setText(task.getIdentifier());
	        txtName.setText(task.getName());
	        //txtPlatforms.setText(task.getP):
	        txtDescription.setText(task.getDescription());
	        txtPrecondition.setText(task.getPreCondition());
	        ckbIteratif.setSelected(task.isIteratif());
	        ckbOptional.setSelected(task.isOptional());
	    
	        // initialize lists
			List<String> categories = task.getCategories();
			Iterator it1 = categories.iterator();
			while (it1.hasNext()){
				cmbCategory.addItem(it1.next());
			}
			List<String> types = task.getTypes();
			Iterator it2 = types.iterator();
			while (it2.hasNext()){
				cmbType.addItem(it2.next());
			}
			List<String> frequencies = task.getFrequencies();
			Iterator it3 = frequencies.iterator();
			while (it3.hasNext()){
				cmbFrequency.addItem(it3.next());
			}
			btnDescription.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {
					ChangeTextDialog dlg = new ChangeTextDialog(getImagCTTE(),txtDescription);
					dlg.setVisible(true);
				}
			});
		}
	}
	/**
	 * Initialize componenets
	 */
	protected void InitComponents(){

        lblIdentifier = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblCategory = new javax.swing.JLabel();
        lblType = new javax.swing.JLabel();
        lblFrequency = new javax.swing.JLabel();
        lblPlatforms = new javax.swing.JLabel();
        lblIteratif = new javax.swing.JLabel();
        lblOptional = new javax.swing.JLabel();
        lblDescription = new javax.swing.JLabel();
        lblPrecondition = new javax.swing.JLabel();
        txtIdentifier = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        txtPlatforms = new javax.swing.JTextField();
        txtDescription = new javax.swing.JTextField();
        txtPrecondition = new javax.swing.JTextField();
        cmbCategory = new javax.swing.JComboBox();
        cmbType = new javax.swing.JComboBox();
        cmbFrequency = new javax.swing.JComboBox();
        ckbOptional = new javax.swing.JCheckBox();
        ckbIteratif = new javax.swing.JCheckBox();
        btnPlatforms = new javax.swing.JButton();
        btnDescription = new javax.swing.JButton();
        btnPrecondition = new javax.swing.JButton();

        lblIdentifier.setText("Identifier:");

        lblName.setText("Name:");

        lblCategory.setText("Category:");

        lblType.setText("Type:");

        lblFrequency.setText("Frequency:");

        lblPlatforms.setText("Platforms:");

        lblIteratif.setText("Iteratif:");

        lblOptional.setText("Optional:");

        lblDescription.setText("Description:");

        lblPrecondition.setText("Precondition:");
        

        btnPlatforms.setText("...");
        btnPlatforms.setMargin(new java.awt.Insets(2, 1, 2, 1));

        btnDescription.setText("...");
        btnDescription.setMargin(new java.awt.Insets(2, 1, 2, 1));

        btnPrecondition.setText("...");
        btnPrecondition.setMargin(new java.awt.Insets(2, 1, 2, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPrecondition)
                    .addComponent(lblDescription)
                    .addComponent(lblOptional)
                    .addComponent(lblIteratif)
                    .addComponent(lblPlatforms)
                    .addComponent(lblFrequency)
                    .addComponent(lblType)
                    .addComponent(lblCategory)
                    .addComponent(lblName)
                    .addComponent(lblIdentifier))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtIdentifier, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbCategory, 0, 157, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbType, 0, 157, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbFrequency, 0, 157, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDescription))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPrecondition, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPrecondition))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtPlatforms, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnPlatforms))
                            .addComponent(ckbOptional)
                            .addComponent(ckbIteratif))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIdentifier)
                    .addComponent(txtIdentifier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblName)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCategory)
                    .addComponent(cmbCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblType)
                    .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFrequency)
                    .addComponent(cmbFrequency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPlatforms)
                    .addComponent(btnPlatforms)
                    .addComponent(txtPlatforms, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblIteratif)
                    .addComponent(ckbIteratif))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblOptional)
                    .addComponent(ckbOptional))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDescription)
                    .addComponent(btnDescription)
                    .addComponent(txtDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPrecondition)
                    .addComponent(btnPrecondition)
                    .addComponent(txtPrecondition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );
	}
	/**
	 * Components
	 */
	private javax.swing.JButton btnDescription;
    private javax.swing.JButton btnPlatforms;
    private javax.swing.JButton btnPrecondition;
    private javax.swing.JCheckBox ckbIteratif;
    private javax.swing.JCheckBox ckbOptional;
    private javax.swing.JComboBox cmbCategory;
    private javax.swing.JComboBox cmbFrequency;
    private javax.swing.JComboBox cmbType;
    private javax.swing.JLabel lblCategory;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblFrequency;
    private javax.swing.JLabel lblIdentifier;
    private javax.swing.JLabel lblIteratif;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblOptional;
    private javax.swing.JLabel lblPlatforms;
    private javax.swing.JLabel lblPrecondition;
    private javax.swing.JLabel lblType;
    private javax.swing.JTextField txtDescription;
    private javax.swing.JTextField txtIdentifier;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPlatforms;
    private javax.swing.JTextField txtPrecondition;
}
